package com.zzh.partnersys.controller;

import com.zzh.partnersys.common.exception.ResultCodeEnum;
import com.zzh.partnersys.common.exception.ResultUtils;
import com.zzh.partnersys.entity.TaskDO;
import com.zzh.partnersys.entity.request.TaskCreateRequest;
import com.zzh.partnersys.entity.request.TaskQueryRequest;
import com.zzh.partnersys.entity.request.TaskUpdateRequest;
import com.zzh.partnersys.entity.vo.TaskVO;
import com.zzh.partnersys.service.TaskService;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 任务表（支撑任务拆分、分配、进度跟踪） 前端控制�? * </p>
 *
 * @author zzh
 * @since 2025-11-01
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 创建任务
     */
    @PostMapping("/create")
    public ResultUtils<TaskDO> createTask(@RequestBody TaskCreateRequest taskCreateRequest, HttpServletRequest request) {
        if (taskCreateRequest == null) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        TaskDO taskDO = taskService.createTask(taskCreateRequest, request);
        return ResultUtils.ok(taskDO);
    }

    /**
     * 更新任务
     */
    @PostMapping("/update")
    public ResultUtils<TaskDO> updateTask(@RequestBody TaskUpdateRequest taskUpdateRequest, HttpServletRequest request) {
        if (taskUpdateRequest == null) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        TaskDO taskDO = taskService.updateTask(taskUpdateRequest, request);
        return ResultUtils.ok(taskDO);
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/delete/{taskId}")
    public ResultUtils<Boolean> deleteTask(@PathVariable Long taskId, HttpServletRequest request) {
        if (taskId == null || taskId <= 0) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        boolean deleted = taskService.deleteTask(taskId, request);
        return ResultUtils.ok(deleted);
    }

    /**
     * 条件查询任务列表
     */
    @PostMapping("/query")
    public ResultUtils<List<TaskVO>> queryTasks(@RequestBody TaskQueryRequest taskQueryRequest, HttpServletRequest request) {
        List<TaskVO> taskVOList = taskService.queryTasks(taskQueryRequest, request);
        return ResultUtils.ok(taskVOList);
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/detail/{taskId}")
    public ResultUtils<TaskVO> getTaskDetail(@PathVariable Long taskId, HttpServletRequest request) {
        if (taskId == null || taskId <= 0) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        TaskVO taskVO = taskService.getTaskDetail(taskId, request);
        return ResultUtils.ok(taskVO);
    }

    /**
     * 更新任务进度
     */
    @PostMapping("/updateProgress")
    public ResultUtils<Boolean> updateTaskProgress(@RequestParam Long taskId, @RequestParam Integer progress, HttpServletRequest request) {
        if (taskId == null || taskId <= 0 || progress == null || progress < 0 || progress > 100) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        boolean updated = taskService.updateTaskProgress(taskId, progress, request);
        return ResultUtils.ok(updated);
    }

    /**
     * 更新任务状�?     */
    @PostMapping("/updateStatus")
    public ResultUtils<Boolean> updateTaskStatus(@RequestParam Long taskId, @RequestParam Integer status, HttpServletRequest request) {
        if (taskId == null || taskId <= 0 || status == null || status < 0 || status > 3) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        boolean updated = taskService.updateTaskStatus(taskId, status, request);
        return ResultUtils.ok(updated);
    }

    /**
     * 查询用户参与的任�?     */
    @GetMapping("/user/{userId}")
    public ResultUtils<List<TaskVO>> getUserTasks(@PathVariable Long userId, HttpServletRequest request) {
        if (userId == null || userId <= 0) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        List<TaskVO> taskVOList = taskService.getUserTasks(userId, request);
        return ResultUtils.ok(taskVOList);
    }

    /**
     * 根据负责人筛选任�?     */
    @GetMapping("/assignee/{assigneeUid}")
    public ResultUtils<List<TaskVO>> getTasksByAssignee(@PathVariable Long assigneeUid, HttpServletRequest request) {
        if (assigneeUid == null || assigneeUid <= 0) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        List<TaskVO> taskVOList = taskService.getTasksByAssignee(assigneeUid, request);
        return ResultUtils.ok(taskVOList);
    }

    /**
     * 查询团队任务
     */
    @GetMapping("/team/{teamId}")
    public ResultUtils<List<TaskVO>> getTeamTasks(@PathVariable Long teamId, HttpServletRequest request) {
        if (teamId == null || teamId <= 0) {
            return ResultUtils.build(null, ResultCodeEnum.PARAMS_ERROR);
        }
        List<TaskVO> taskVOList = taskService.getTeamTasks(teamId, request);
        return ResultUtils.ok(taskVOList);
    }
}

